package com.detroitchow2.vo;

import java.io.Serializable;
import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

import com.detroitchow2.util.AppUtil;

public class StoreVO 
implements Comparable<StoreVO>, Serializable, Parcelable
{

  private static final long serialVersionUID = 1L;
  public final String NEW_LINE="\n";
  public final String EMPTY_SPACE=" ";
  public int storeId;
  public String name;
  public String descr;
  public String address;
  public String city;
  public String state;
  public String zip;
  public String lat;
  public String lng;
  public String phone;
  public String facebook;
  public String twitter;
  public String email;
  public String googlePlus;
  public String website;
  public String shortReview;
  public int rank=0;
  public ArrayList<String> tags;
  public int isListItem;
  public int mapGroupId;
  public String mapDescr;
  
  public StoreVO()
  {
    
  }
  
  public String getMapTitle() {
    if(this.name == null)
      return "";
    else
      return this.name;
  }
  
  public String getSnippetLayout() {
    String result="";
    
    if(isLegitValue(this.mapDescr))
      result = this.mapDescr + NEW_LINE + EMPTY_SPACE + NEW_LINE;
    
    if(isLegitValue(this.address))
      result += this.address;
    
    if(isLegitValue(this.city)  && isLegitValue(this.state) && isLegitValue(this.zip))
      result += NEW_LINE + this.city + EMPTY_SPACE + this.state + EMPTY_SPACE + this.zip;
    else if(this.city != null)
      result += NEW_LINE + this.city;
    
    if(isLegitValue(this.phone)) {
      result += NEW_LINE + AppUtil.formattedPhoneNumber(this.phone);
    }
    
    if(isLegitValue(this.website)) {
      result += NEW_LINE + this.website;
    }
    
    return result;
  }
  
  public boolean isLegitValue(String s){
    if(s == null)
      return false;
    if(s.length() < 1)
      return false;
    return true;
  }
  
  public String toString() {
    String result = "<StoreVO>\n<storeId>" + this.storeId + "</storeId>" + NEW_LINE;
    result += "<name>" + this.name + "</name>" + NEW_LINE;
    result += "<descr>" + this.descr + "</descr>" + NEW_LINE;
    result += "<lat>" + this.lat + "</lat>" + NEW_LINE;
    result += "<lng>" + this.lng + "</lng>" + NEW_LINE;
    result += "<address>" + this.address + "</address>" + NEW_LINE;
    result += "<city>" + this.city + "</city>" + NEW_LINE;
    result += "<zip>" + this.zip + "</zip>" + NEW_LINE;
    result += "<webSite>" + this.website + "</webSite>" + NEW_LINE;
    result += "</StoreVO>";
    
    return result;
  }
  
  public int compareTo(StoreVO another) {
    // TODO Auto-generated method stub
    return this.name.compareTo(another.name);
  }

  public String getFormattedPhone()
  {
    if(this.phone == null)
      return "";
    
    String result = null;
    if(this.phone.length() ==10) {
      
      result ="(" + this.phone.substring(0, 3) + ") ";
      result += this.phone.substring(3,6) + "-";
      result += this.phone.substring(6,10);
      
    }else if(this.phone.length() ==7) {
      
      result = this.phone.substring(0,3) + "-";
      result += this.phone.substring(3,7);
      
    }else
      result = this.phone;
    return result;
  }

  @Override
  public int describeContents()
  {
    // TODO Auto-generated method stub
    return this.storeId;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags)
  {
    dest.writeInt(this.storeId);
    dest.writeInt(this.mapGroupId);
    dest.writeString(this.name);
    dest.writeString(this.address);
    dest.writeString(this.city);
    dest.writeString(this.descr);
    dest.writeString(this.email);
    dest.writeString(this.lat);
    dest.writeString(this.lng);
    dest.writeString(this.mapDescr);
    dest.writeString(this.phone);
    dest.writeString(this.website);
    dest.writeString(this.zip);
    dest.writeInt(this.isListItem);
    
  }
  
  public StoreVO(Parcel source){
      /*
       * Reconstruct from the Parcel
       */
      
      this.storeId = source.readInt();
      this.name = source.readString();
      this.address = source.readString();
      this.city = source.readString();
      this.descr = source.readString();
      this.email = source.readString();
      this.lat = source.readString();
      this.isListItem = source.readInt();
      this.mapDescr = source.readString();
      this.mapGroupId = source.readInt();
      this.lng = source.readString();
      this.phone = source.readString();
      this.state = source.readString();
      this.website = source.readString();
      this.zip =  source.readString();
      
  }
  
  public String getPhoneIntentFormat()
  {
    return "tel:" + this.phone;
  }
  
  public static Parcelable.Creator<StoreVO> CREATOR = new Parcelable.Creator<StoreVO>() 
  {
    public StoreVO createFromParcel(Parcel in) {
      return new StoreVO(in);
    }

  public StoreVO[] newArray(int size) {
    return new StoreVO[size];
  }
};

 
}
